---
title: "Replication File (Data Processing, Survey 2, Wave 1): The Limits of Public Support for Fiscal Consolidation: Survey Evidence from Great Britain"
author: "Björn Bremer, Charlotte Cavaille, Lisanne de Blok, Catherine de Vries"
date: "February 3, 2026"
output:
  bookdown::html_document2:
    keep_tex: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(warning = FALSE)
```

```{r, include=FALSE}
rm(list = ls())
set.seed(1234567)

# List of packages needed
packages <- c("tidyverse"
)

# Function to check if packages are installed and install if needed
package_check <- function(pkg){
  if (!require(pkg, character.only = TRUE)) {
    install.packages(pkg, dependencies = TRUE)
    library(pkg, character.only = TRUE)
  }
}

# Apply function to each package
invisible(sapply(packages, package_check))

# Create directories to save data
if (!dir.exists("data")) {
  dir.create("data")
}
```

```{r, include=FALSE}
## clean the data
debt<- read.csv("Debt & Preferences - June - Wave 1_August 3, 2022_09.16.csv", colClasses = c("character"), sep = ",")
debt <- debt[-c(1:2),] # remove unnecessary columns

# change the variable names
names(debt) <- gsub(x = names(debt), pattern = "\\.", replacement = "")  

# creating dummy for speeders
debt$duration <- as.numeric(debt$Durationinseconds)
debt$speeder <- ifelse(debt$duration<306 & debt$treat==1 |debt$duration<364 & debt$treat==2 |debt$duration<323 & debt$treat==3, 1, 0)
table(debt$speeder)
# control: 306 seconds
# treatmetn 1: 364 seconds
# treatment 2: 323 seconds

# create a measure for wether respondents passed the sound check and the attention check and did not speed
debt$Finished <- as.numeric(debt$Finished)
debt$soundC <- as.numeric(debt$soundC)
debt$AC <- as.numeric(debt$AC)
debt$SC_passed <- ifelse(debt$soundC==3, 1, 0)
debt$AC_passed <- ifelse(debt$AC==8, 1, 0)
debt$check <- ifelse(debt$speeder==0 & debt$soundC==3 & debt$AC==8, 1, 0)
```

```{r controlvariables, include=FALSE}
## recode control variables
debt$age <-2021-as.numeric(debt$yrbrn)
debt$age_rec <- ifelse(debt$age<35, "Up to 35", ifelse(debt$age>=35 & debt$age<51, "35-50", ifelse(debt$age>=51 & debt$age<65, "51-65", ifelse(debt$age>=65, "65+", NA))))

# gender
debt$gender_rec <- ifelse(debt$gender==1, "Female", ifelse(debt$gender==2 |debt$gender==3, "Male", NA))

#education
debt$edu_rec <- ifelse(debt$educ %in% c(1, 2, 3), "Low",
                 ifelse(debt$educ == 4, "Middle",
                 ifelse(debt$educ %in% c(5, 6, 7, 8, 9), "High", NA)))

#employment
debt$emp <- as.numeric(debt$emp)
debt$emp_rec <- ifelse(debt$emp==10 | debt$emp==11 | debt$emp==16, "Employed or student",
                       ifelse(debt$emp==12 | debt$emp==13, "Unemployed", 
                              ifelse(debt$emp==15, "Retired", 
                                     ifelse(debt$emp==14 | debt$emp ==17 | debt$emp == 18, "Out of work force", NA))))

#income
debt$income <- debt$hhinc_w
debt$income <- ifelse(debt$income=="", debt$hhinc_m, debt$income)
debt$income <- ifelse(debt$income=="", debt$hhinc_y, debt$income)
debt$income <- ifelse(debt$income=="" | debt$income=="99" | debt$income=="88", NA, debt$income)
debt$income <- as.numeric(debt$income)
debt$income_cat <- ifelse(debt$income<=3, "Low",
                          ifelse(debt$income==4 |debt$income==5 |debt$income==6 |debt$income==7, "Middle",
                                 ifelse(debt$income>=8, "High", NA)))

# political variables / mediators vote
debt$vote_part1 <- as.numeric(debt$vote_part1)
debt$vote_part2 <- as.numeric(debt$vote_part2)

debt$party_1 <- ifelse(debt$vote_part1 == 1 | debt$vote_part1 == 7 | debt$vote_part1 == 8, "Right-wing",
                       ifelse(debt$vote_part1 == 2 | debt$vote_part1 == 4 | debt$vote_part1 == 5 | debt$vote_part1 == 6, "Left-wing",
                              ifelse(debt$vote_part1 == 3, "Lib Dems",
                                     ifelse(debt$vote_part1 == 9 | debt$vote_part1 == 88, "Other", NA))))

debt$party_2 <- ifelse(debt$vote_part2 == 1 | debt$vote_part2 == 5 | debt$vote_part2 == 7 | debt$vote_part2 == 8, "Right-wing",
                       ifelse(debt$vote_part2 == 2 | debt$vote_part2 == 4 | debt$vote_part2 == 6, "Left-wing",
                              ifelse(debt$vote_part2 == 3, "Lib Dems",
                                     ifelse(debt$vote_part2 == 9 | debt$vote_part2 == 88, "Other", NA))))
debt$party <- debt$party_1
debt$party <- ifelse(debt$party=="" | is.na(debt$party), debt$party_2, debt$party)

# partisanship variable with only Labour vs. Conservatives
debt$party_1_rec <- ifelse(debt$vote_part1 == 1, "Conservatives",
                       ifelse(debt$vote_part1 == 2, "Labour", NA))

debt$party_2_rec <- ifelse(debt$vote_part2 == 1, "Conservatives",
                       ifelse(debt$vote_part2 == 2, "Labour", NA))
debt$party_rec <- debt$party_1_rec
debt$party_rec <- ifelse(debt$party_rec=="" | is.na(debt$party_rec), debt$party_2_rec, debt$party_rec)

debt$party_1_rec2 <- ifelse(debt$vote_part1 == 1, "Conservatives",
                       ifelse(debt$vote_part1 == 2, "Labour",
                       ifelse(debt$vote_part1 == 3, "Lib Dem",
                       ifelse(debt$vote_part1 == 4, "SNP",
                       ifelse(debt$vote_part1 == 98 |debt$vote_part1 == 99, NA, "Other")))))
debt$party_2_rec2 <- ifelse(debt$vote_part2 == 1, "Conservatives",
                       ifelse(debt$vote_part2 == 2, "Labour",
                       ifelse(debt$vote_part2 == 3, "Lib Dem",
                       ifelse(debt$vote_part2 == 4, "SNP",
                       ifelse(debt$vote_part2 == 98 |debt$vote_part2 == 99, NA, "Other")))))
debt$party_rec2 <- debt$party_1_rec2
debt$party_rec2 <- ifelse(debt$party_rec2=="" | is.na(debt$party_rec2), debt$party_2_rec2, debt$party_rec2)
```

```{r outcomevariables, include=FALSE}
## recode outcome variables

# concern over debt
debt$debt_pre <- debt$MC_debt_V1_1 
debt$debt_pre <- ifelse(debt$debt_pre =="", debt$MC_debt_V1_2, debt$debt_pre)
debt$debt_pre <- ifelse(debt$debt_pre =="", debt$MC_debt_V2_3, debt$debt_pre)
debt$debt_pre <- ifelse(debt$debt_pre =="", debt$MC_debt_V2_4, debt$debt_pre)
debt$debt_pre <- as.numeric(debt$debt_pre)

# concern over china
debt$china_pre <- debt$pb_china_V1_1 
debt$china_pre <- ifelse(debt$china_pre =="", debt$pb_china_V1_2, debt$china_pre)
debt$china_pre <- ifelse(debt$china_pre =="", debt$pb_china_V2_3, debt$china_pre)
debt$china_pre <- ifelse(debt$china_pre =="", debt$pb_china_V2_4, debt$china_pre)
debt$china_pre <- as.numeric(debt$china_pre)

# concern over climate
debt$climate_pre <- debt$pb_climate_V1_1 
debt$climate_pre <- ifelse(debt$climate_pre =="", debt$pb_climate_V1_2, debt$climate_pre)
debt$climate_pre <- ifelse(debt$climate_pre =="", debt$pb_climate_V2_3, debt$climate_pre)
debt$climate_pre <- ifelse(debt$climate_pre =="", debt$pb_climate_V2_4, debt$climate_pre)
debt$climate_pre <- as.numeric(debt$climate_pre)

# concern over immigration
debt$immi_pre <- debt$pb_immi_V1_1 
debt$immi_pre <- ifelse(debt$immi_pre =="", debt$pb_immi_V1_2, debt$immi_pre)
debt$immi_pre <- ifelse(debt$immi_pre =="", debt$pb_immi_V2_3, debt$immi_pre)
debt$immi_pre <- ifelse(debt$immi_pre =="", debt$pb_immi_V2_4, debt$immi_pre)
debt$immi_pre <- as.numeric(debt$immi_pre)

# debt after treatment (treatment)
debt$debt_post <- debt$MC_debt_V1_post
debt$debt_post <- ifelse(debt$debt_post =="", debt$MC_debt_V2_post, debt$debt_post)

# china after treatment (placebo)
debt$china_post <- debt$pb_china_V1_post 
debt$china_post <- ifelse(debt$china_post =="", debt$pb_china_v2_post, debt$china_post)

# policy index
debt$pol1_rec <- ifelse(debt$pol1=="", NA, debt$pol1)
debt$pol2_rec <- ifelse(debt$pol2=="", NA, debt$pol2)
debt$pol3_rec <- ifelse(debt$pol3=="", NA, debt$pol3)
debt$pol1_rec <- as.numeric(debt$pol1_rec)
debt$pol2_rec <- as.numeric(debt$pol2_rec)
debt$pol3_rec <- as.numeric(debt$pol3_rec)

debt$debt_index <- debt$pol1_rec + debt$pol2_rec + debt$pol3_rec

# debt vs. climate change
debt$debt_CC <- debt$prioD_CCa
debt$debt_CC <- ifelse(debt$debt_CC=="", debt$prioD_CCb, debt$debt_CC)
debt$debt_CC <- as.numeric(debt$debt_CC)

debt$debt_CC_rec <- ifelse(debt$debt_CC == 2, 1, 
                          ifelse(debt$debt_CC == 1, 0, NA))

# debt vs. immigration
debt$debt_immi <- debt$prioD_immia
debt$debt_immi <- ifelse(debt$debt_immi=="", debt$prioD_immib, debt$debt_immi)
debt$debt_immi <- as.numeric(debt$debt_immi)

debt$debt_immi_rec <- ifelse(debt$debt_immi == 2, 1, 
                          ifelse(debt$debt_immi == 1, 0, NA))

# debt vs. nhs
debt$debt_nhs <- debt$prioD_NHSa
debt$debt_nhs <- ifelse(debt$debt_nhs=="", debt$prioD_NHSb, debt$debt_nhs)
debt$debt_nhs <- as.numeric(debt$debt_nhs)

debt$debt_nhs_rec <- ifelse(debt$debt_nhs == 2, 1, 
                          ifelse(debt$debt_nhs == 1, 0, NA))

# debt allocation
debt$alloc_debt <- as.numeric(debt$alloc_1)
debt$alloc_cuts_edu <- as.numeric(debt$alloc_2)
debt$alloc_cuts_benefits <- as.numeric(debt$alloc_3)
debt$alloc_tax_income <- as.numeric(debt$alloc_4)
debt$alloc_tax_vat <- as.numeric(debt$alloc_5)

# expectations 
debt$exp_gdp <- ifelse(debt$gdpR_1 == "", NA, debt$gdpR_1)
debt$exp_infl <- ifelse(debt$inflR_13 == "", NA, debt$inflR_13)
debt$exp_tax <- ifelse(debt$taxR_17 == "", NA, debt$taxR_17)
debt$exp_pen <- ifelse(debt$returnR_13 == "", NA, debt$returnR_13)

debt$exp_nhs_costs <- debt$nhs1a
debt$exp_nhs_costs <- ifelse(debt$exp_nhs_costs=="", debt$nhs1b, debt$exp_nhs_costs)
debt$exp_nhs_costs <- ifelse(debt$exp_nhs_costs == "", NA, debt$exp_nhs_costs)

debt$exp_nhs_qual <- debt$nhs2a
debt$exp_nhs_qual <- ifelse(debt$exp_nhs_qual=="", debt$nhs2b, debt$exp_nhs_qual)
debt$exp_nhs_qual <- ifelse(debt$exp_nhs_qual == "", NA, debt$exp_nhs_qual)

cols <- c("exp_gdp","exp_infl", "exp_tax", "exp_pen")
debt[,cols] <- data.frame(apply(debt[cols], 2, as.numeric))

# certainty on debt
debt$cert_debt <- debt$cert_debt_V1_1 
debt$cert_debt <- ifelse(debt$cert_debt =="", debt$cert_debt_V1_2, debt$cert_debt)
debt$cert_debt <- ifelse(debt$cert_debt =="", debt$cert_debt_V2_3, debt$cert_debt)
debt$cert_debt <- ifelse(debt$cert_debt =="", debt$cert_debt_V2_4, debt$cert_debt)
debt$cert_debt <- as.numeric(debt$cert_debt)

debt$cert_debt_rec <- ifelse(debt$cert_debt==3, "Serious problem",
                             ifelse(debt$cert_debt==2, "Uncertain",
                                    ifelse(debt$cert_debt==1, "Not a serious problem", NA)))
debt$cert_debt_rec <- as.factor(debt$cert_debt_rec)


# having thought about debt
debt$think_debt <- debt$think_debt_V1_1 
debt$think_debt <- ifelse(debt$think_debt =="", debt$think_debt_V2_2, debt$think_debt)
debt$think_debt <- ifelse(debt$think_debt =="", debt$think_debt_V1_3, debt$think_debt)
debt$think_debt <- ifelse(debt$think_debt =="", debt$think_debt_V2_4, debt$think_debt)
debt$think_debt <- as.numeric(debt$think_debt)
debt$think_debt_rec <- ifelse(debt$think_debt==4, "A great deal",
                          ifelse(debt$think_debt==3, "Moderate amount",
                             ifelse(debt$think_debt==2, "A little",
                                    ifelse(debt$think_debt==1, "Never", NA))))
debt$think_debt_rec <- as.factor(debt$think_debt_rec)

# certainty on climate
debt$cert_climate <- debt$cert_climate_V1_1 
debt$cert_climate <- ifelse(debt$cert_climate =="", debt$cert_climate_V1_2, debt$cert_climate)
debt$cert_climate <- ifelse(debt$cert_climate =="", debt$cert_climate_V2_3, debt$cert_climate)
debt$cert_climate <- ifelse(debt$cert_climate =="", debt$cert_climate_V2_4, debt$cert_climate)
debt$cert_climate <- as.numeric(debt$cert_climate)
debt$cert_climate_rec <- ifelse(debt$cert_climate==3, "Serious problem",
                             ifelse(debt$cert_climate==2, "Uncertain",
                                    ifelse(debt$cert_climate==1, "Not a serious problem", NA)))
debt$cert_climate_rec <- as.factor(debt$cert_climate_rec)

# certainty on immigration
debt$cert_immi <- debt$cert_immi_V1_1 
debt$cert_immi <- ifelse(debt$cert_immi =="", debt$cert_immi_V1_2, debt$cert_immi)
debt$cert_immi <- ifelse(debt$cert_immi =="", debt$cert_immi_V2_3, debt$cert_immi)
debt$cert_immi <- ifelse(debt$cert_immi =="", debt$cert_immi_V2_4, debt$cert_immi)
debt$cert_immi <- as.numeric(debt$cert_immi)
debt$cert_immi_rec <- ifelse(debt$cert_immi==3, "Serious problem",
                             ifelse(debt$cert_immi==2, "Uncertain",
                                    ifelse(debt$cert_immi==1, "Not a serious problem", NA)))
debt$cert_immi_rec <- as.factor(debt$cert_immi_rec)

# certainty on china
debt$cert_china <- debt$cert_china_V1_1 
debt$cert_china <- ifelse(debt$cert_china =="", debt$cert_china_V1_2, debt$cert_china)
debt$cert_china <- ifelse(debt$cert_china =="", debt$cert_china_V2_3, debt$cert_china)
debt$cert_china <- ifelse(debt$cert_china =="", debt$cert_china_V2_4, debt$cert_china)
debt$cert_china <- as.numeric(debt$cert_china)
debt$cert_china_rec <- ifelse(debt$cert_china==3, "Serious problem",
                             ifelse(debt$cert_china==2, "Uncertain",
                                    ifelse(debt$cert_china==1, "Not a serious problem", NA)))
debt$cert_china_rec <- as.factor(debt$cert_china_rec)
```

```{r, include=FALSE}
# coding all the items that are new to wave 2

# debt vs. inflation
debt$debt_infl <- debt$prioD_infla
debt$debt_infl <- ifelse(debt$debt_infl=="", debt$prioD_inflb, debt$debt_infl)
debt$debt_infl <- as.numeric(debt$debt_infl)

debt$debt_infl_rec <- ifelse(debt$debt_infl == 2, 1, 
                          ifelse(debt$debt_infl == 1, 0, NA))

# treatment selection question for control group
debt$trtselect <- ifelse(debt$control_video_yn1==1, "Unsustainable", ifelse(debt$control_video_yn1==2, "Sustainable", ifelse(debt$control_video_yn1==6, "Neither nor", NA)))

# variable whether respondents want to write a letter
table(debt$letter_intro)
```

```{r, include=FALSE}
# z standardization of variables
debt <- debt %>% 
  mutate(across(c(debt_post, china_post, debt_index, debt_nhs_rec, debt_CC_rec, debt_immi_rec, debt_infl_rec, alloc_debt, alloc_cuts_edu, alloc_cuts_benefits, alloc_tax_income, alloc_tax_vat), ~ as.numeric(.))) %>%
  mutate(across(c(debt_post, china_post, debt_index, debt_nhs_rec, debt_CC_rec, debt_immi_rec, debt_infl_rec, alloc_debt, alloc_cuts_edu, alloc_cuts_benefits, alloc_tax_income, alloc_tax_vat),
                ~ as.vector(scale(.)), 
                .names = "{.col}_z"))

# cleaning wave 2
debt_w2 <- debt
rm(debt)

debt_w2 <- debt_w2[which(duplicated(debt_w2$respid)==F),]
debt_w2$check_rec <- ifelse(debt_w2$speeder==0 & debt_w2$soundC==3 & debt_w2$AC==8 & debt_w2$Finished==1, 1, 0)
debt_w2 <- debt_w2[which(debt_w2$check_rec==1),]

debt_w2$treat <- as.factor(debt_w2$treat) # treatment as factor variable

debt_w2 <- debt_w2 %>% dplyr::select(respid, treat, debt_post, debt_post_z, block, debt_pre, edu_rec, party, debt_index, debt_index_z, alloc_debt, alloc_debt_z, alloc_cuts_edu, alloc_cuts_edu_z, alloc_cuts_benefits, alloc_cuts_benefits_z, alloc_tax_income, alloc_tax_income_z, alloc_tax_vat, alloc_tax_vat_z, debt_CC_rec, debt_CC_rec_z, debt_immi_rec, debt_immi_rec_z, debt_nhs_rec, debt_nhs_rec_z, exp_gdp, exp_infl, exp_nhs_costs, exp_nhs_qual, exp_pen, exp_tax, debt_infl_rec, debt_infl_rec_z, cert_debt_rec, china_pre, china_post, china_post_z, cert_climate_rec, cert_immi_rec, cert_china_rec, immi_pre, climate_pre, age, age_rec, gender_rec, income_cat, think_debt_rec, emp_rec, pol1_rec, pol2_rec, pol3_rec, party_rec, letter_intro, MC_US, MC_S, rating1_S, rating2_S, rating1_US, rating2_US, income, party_rec2, trtselect)
debt_w2 <- debt_w2 %>%  rename_all(paste0, "_w2")
debt_w2$respid <- debt_w2$respid_w2
debt_w2$treat <- debt_w2$treat_w2
```

```{r, include=FALSE}
## merge coding of letter writing

# load the letter writing data from CSV
letter_data <- read_csv("letter_writing_subset.csv")

# merge the datasets
debt_w2 <- debt_w2 %>%
  left_join(letter_data, by = "respid")

debt_w2 <- debt_w2 %>%
  rename(prioDebt = priod_justi,
         prioSupp = more_help_n_spending)

```

```{r, include=FALSE}
save(debt_w2, file = "data/data_processed_w2.RData")
```